/**
 * @ author: 汪孟苹
 */
define(['jquery',"bbcare","comm/form/form","bch/app/specialSchemeRegister/lapseToAdd.servers"],function($,$bb,$form,$servers){
	var addPatient = {};

	/**
	 * 新增专案登记成功后的回调函数
	 */
	var ajaxArchiveCallBack = function(data){
		$bb.toastShow("新增成功！");
		var params = $bb.getPageParams();
        $bb.open(params,"saveBtn","bch_lapseToList");
	}

	/**
	 * 新增专案登记
	 */
	var onArchiveSave = function(){
		if( $form.validate("archiveBox") ){
			var params = $(this).data("params");
			var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
			var contList = $form.getSaveData("archiveBox");
			var userName = $bb.code.getValueByName("archiveBox","userName");
			var channelId = $bb.getChannelId();
			var taskIdList = [params.taskId];
			var params1 = {
				"oprInfo":{
					"opCode":"1000",
					"opType":"2",
					"opNote":"【"+doctorName+"】医生添加【"+userName+"】患者",
					"channelType":"2", // web端门诊渠道:2
					"channelId":channelId,
				},
				"archiveInfo":{
					"queueId":params.queueId,
					"schemeId":params.schemeId,
					"contList":contList,
					"taskIdList":taskIdList,
				},
				"isReg":"isReg"  // 专案登记建档  多这个参数
			};
			$servers.ajaxArchive(params1,ajaxArchiveCallBack);
		}
	}

	/**
	 * 请求队列成功后的回调函数
	 */
	var ajaxRegUserTemplCallBack = function(data,params){
		if( !params.questions && data.rows && data.rows.length > 0 ) params.questions = data.rows;
		if( params.questions && params.questions.length > 0 ){
			$form.init({
				"id":"archiveBox",
				"questions":params.questions,
				"values":data.valueList,
				"event":{
					"autoCompleteCallBack":ajaxRegUserTemplCallBack,
					"autoCompleteParams":params
				}
			});
			// 保存
			var topbars = [
				{
					"id":"saveBtn",
					"icon":"fa fa-floppy-o",
					"name":"保存",
					"params":params,
					"callback":onArchiveSave
				}
			];
	        $bb.toolbar(topbars);
		}
	}

	/**
	 * 建档任务查询成功后的回调函数
	 */
	var ajaxArchiveTaskCallBack = function(data,params){
		if( data.taskList.length > 0 ){
			var arr = [];
			for (var i = 0; i < data.taskList.length; i++) {
				if( data.taskList[i].busiType != "A0801" ){
					arr.push(data.taskList[i]);
				}
			}
			// 若以后建档配置了多个任务，需再调整
			if( arr.length == 1 ){
				$.extend(params,{"schemeVersionId":arr[0].schemeVersionId,"taskId":arr[0].id});
				$servers.ajaxTaskContTempl(params,ajaxRegUserTemplCallBack);
			}
		}
	}

	/**
	 * 建档页面初始化
	 */
	addPatient.init = function(){
		var params = $bb.getPageParams();
		if( params.title ) $("#lapseToAddTit").html(params.title + "新增");
		$servers.ajaxArchiveTask(params,ajaxArchiveTaskCallBack);
	};

	return addPatient;
});